import express from "express";
import { spider } from "./spider-playwhite.js";
import { minify } from "html-minifier";

const app = express();
app.get("*", async (req, res) => {
  let fullUri = req.originalUrl;
  // let fullUri = "/";
  // 拼装待抓取资源全路径
  const url = `http://localhost:18081${fullUri}`;
  console.log("需抓取的URL：" + url);
  let content = await spider(url).catch((error) => {
    console.log(error);
    res.send("获取html内容失败");
    return;
  });
  // 通过minify库压缩代码
  content = minify(content, {
    removeComments: true,
    collapseWhitespace: true,
    minifyJS: true,
    minifyCSS: true,
  });
  res.send(content);
});
app.listen(4321, "0.0.0.0", () => {
  console.log("server running http://localhost:4321");
});
// app.on("close", () => {
//   console.log("服务被关闭");
// });
// process.on("SIGINT", function () {
//   console.log("Got SIGINT.  Press Control-D/Control-C to exit.");
// });
